package comm

import (
	models "GoGin/Models"
	service "GoGin/Service"
	"fmt"
	"log"

	"github.com/facebookgo/inject"
)

type InjectIoc struct {
	injector inject.Graph
}

func (i *InjectIoc) Init() {
	userInfo := &models.User{Id: 1, Name: "测试", LoginName: "测试"}
	if err := i.injector.Provide(
		&inject.Object{Value: &service.ATest{}},
		&inject.Object{Value: &service.BTest{}},
		&inject.Object{Value: userInfo},
	); err != nil {
		fmt.Println("inject fatal: ", err)
	}
	if err := i.injector.Populate(); err != nil {
		log.Fatal("injector fatal: ", err)
	}
}

func (i *InjectIoc) Upd(a ...any) {
	userInfo := &models.User{Id: 1, Name: "测试2", LoginName: "测试2"}
	injectObj := make([]*inject.Object, 1)
	injectObj[0] = &inject.Object{Value: userInfo}
	for _, o := range a {
		injectObj = append(injectObj, &inject.Object{Value: o})
	}
	if err := i.injector.Provide(injectObj...); err != nil {
		fmt.Println("inject fatal: ", err)
	}
	if err := i.injector.Populate(); err != nil {
		log.Fatal("injector fatal: ", err)
	}
}
